python

您所在的位置:网站首页 Python-docx 渐变色 python

python

2023-12-18 13:50| 来源: 网络整理| 查看: 265

因需要添加word封面图,并未找到相关资料直接添加封面图,操作word插入封面图后想到一个解决办法就是插入图片修改大小使其成为封面图。 查找资料后遇到一个问题就是如何修改图片环绕方式,经多方查找后找到一篇文章,简单的思路就是添加xml

docx背后的xml

我们还知道,docx文档的背后是xml格式的数据,python-docx正是通过处理xml的方式来读写word文档。所以,接下来先手工创建word文档,然后查看图片部分的xml内容。

作为对比,首先分别创建一个普通嵌入型图片文件和一个衬于文本下方的浮动型图片文件。然后执行查看步骤:右键docx文件 | 7-zip打开压缩包 | word | document.xml,复制文件内容并格式化xml,得到如下的关于图片部分的片段。为了便于对比分析,删除了一些节点属性。(具体内容文章底部会有源文链接)

pip install python-docx

# -*- coding: utf-8 -*- # filename: add_float_picture.py ''' Implement floating image based on python-docx. - Text wrapping style: BEHIND TEXT - Picture position: top-left corner of PAGE ``. Create a docx sample (Layout | Positions | More Layout Options) and explore the source xml (Open as a zip | word | document.xml) to implement other text wrapping styles and position modes per `CT_Anchor._anchor_xml()`. ''' from docx.oxml import parse_xml, register_element_cls from docx.oxml.ns import nsdecls from docx.oxml.shape import CT_Picture from docx.oxml.xmlchemy import BaseOxmlElement, OneAndOnlyOne # refer to docx.oxml.shape.CT_Inline class CT_Anchor(BaseOxmlElement): """ ```` element, container for a floating image. """ extent = OneAndOnlyOne('wp:extent') docPr = OneAndOnlyOne('wp:docPr') graphic = OneAndOnlyOne('a:graphic') @classmethod def new(cls, cx, cy, shape_id, pic, pos_x, pos_y): """ Return a new ```` element populated with the values passed as parameters. """ anchor = parse_xml(cls._anchor_xml(pos_x, pos_y)) anchor.extent.cx = cx anchor.extent.cy = cy anchor.docPr.id = shape_id anchor.docPr.name = 'Picture %d' % shape_id anchor.graphic.graphicData.uri = ( 'http://schemas.openxmlformats.org/drawingml/2006/picture' ) anchor.graphic.graphicData._insert_pic(pic) return anchor @classmethod def new_pic_anchor(cls, shape_id, rId, filename, cx, cy, pos_x, pos_y): """ Return a new `wp:anchor` element containing the `pic:pic` element specified by the argument values. """ pic_id = 0 # Word doesn't seem to use this, but does not omit it pic = CT_Picture.new(pic_id, filename, rId, cx, cy) anchor = cls.new(cx, cy, shape_id, pic, pos_x, pos_y) anchor.graphic.graphicData._insert_pic(pic) return anchor @classmethod def _anchor_xml(cls, pos_x, pos_y): return ( '\n' ' \n' ' \n' ' %d\n' ' \n' ' \n' ' %d\n' ' \n' ' \n' ' \n' ' \n' ' \n' ' \n' ' \n' ' \n' ' \n' ' \n' '' % (nsdecls('wp', 'a', 'pic', 'r'), int(pos_x), int(pos_y)) ) # refer to docx.parts.story.BaseStoryPart.new_pic_inline def new_pic_anchor(part, image_descriptor, width, height, pos_x, pos_y): """Return a newly-created `w:anchor` element. The element contains the image specified by *image_descriptor* and is scaled based on the values of *width* and *height*. """ rId, image = part.get_or_add_image(image_descriptor) cx, cy = image.scaled_dimensions(width, height) shape_id, filename = part.next_id, image.filename return CT_Anchor.new_pic_anchor(shape_id, rId, filename, cx, cy, pos_x, pos_y) # refer to docx.text.run.add_picture def add_float_picture(p, image_path_or_stream, width=None, height=None, pos_x=0, pos_y=0): """Add float picture at fixed position `pos_x` and `pos_y` to the top-left point of page. """ run = p.add_run() anchor = new_pic_anchor(run.part, image_path_or_stream, width, height, pos_x, pos_y) run._r.add_drawing(anchor) # refer to docx.oxml.__init__.py register_element_cls('wp:anchor', CT_Anchor) document = Document(path) pic = document.paragraphs[0] #其实位置添加 add_float_picture(pic, img_path, width=Cm(21.29), height=Cm(30.18), pos_x=Cm(0.11), pos_y=Cm(-0.2)) # 判断文件是否存在,不存在则创建 save_file_path = settings.MEDIA_ROOT + f'/userfile/{userID}/report_ini/' if not os.path.exists(save_file_path): os.makedirs(save_file_path) save_file_name = get_save_filename() document.save(save_file_path + save_file_name)

源文地址:https://www.zhangshengrong.com/p/OgN5D9zDan/



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3